import { Router } from "express";
import { IcallbackFn, Ioracle } from "../../types";

export default (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {

  router.post("/refreshCanteenData",(_req,res) => {
    //更新资料
    let sql = " begin xx_g00_canteen_data_p;end; "
    callbackFn(res,oracle.connHrm,{sql})
  })

  router.post("/refreshCanteenDataNoGC",(_req,res) =>{
    //更新資料--不更新GC資料
    let sql = "begin xx_g00_canteen_data_p2;  end;"
    callbackFn(res,oracle.connHrm,{sql})
  })

    //獲取GCFlag,是否可執行存儲過程
  router.post("/getGCFlag",(_req,res) => {
    let sql = " select gc_flag from xx_g00_canteen_updateGC_flag"
    sql += " where created_by = 'xiaowei'"
    callbackFn( res, oracle.connHrm,{sql})
  })
  //手動點擊G->C/C->G,設置Flag
  router.post("/updateGCFlag",(_req,res) => {
    const {dormno} = _req.query
    let flag:string = "Y"
    if (dormno == "C"){
       flag = "N"
    }
    else if(dormno == "G"){
       flag = "Y"
    }
    let sql = "update xx_g00_canteen_updateGC_flag"
    sql += " set gc_flag = '" + flag + "'"
    sql += " ,record_date = sysdate"
    sql += " where created_by = 'xiaowei'"
    callbackFn( res, oracle.connHrm,{sql})
  })

   //更改C->G時，獲取工程師宿舍名單添加附屬宿舍獲得幹部宿舍門禁權限
   router.post("/getCToGDormData" ,(_req,res) => {
    let sql = "select * from xx_g00_canteen_data"
    sql += " where post_flag = 'N'"
    sql += " and dorm_no = 'G'"
    sql += " and resign_flag is null"
    callbackFn(res,oracle.connHrm,{sql})
  })

    //更改G->C時，獲取工程師宿舍名單已有幹部宿舍權限並取消掉
  router.post("/deleteCToGDormData" ,(_req,res) => {
    let sql = "select * from xx_g00_canteen_data "
    sql += " where dorm_no = 'C'"
    sql += " and work_num in ( select badge_no from chaos.xx_g00_employee_card@ccprod "
    sql += " where line_id in (select line_id from chaos.xx_g00_bed@ccprod"
    sql += " where header_id = '1029'))"
    sql += " and resign_flag is null"
    callbackFn(res,oracle.connHrm,{sql})
  })
  //抓取需要新增/更新
  router.post("/getCanteenData", (_req, res) => {
    let sql = " select substr(b.dept_code,1,2) org_code,b.dept_code,a.work_num,b.name_in_chinese,a.title,a.is_pregnant "
    sql += " ,a.dorm_no,a.card_no,a.post_flag "
    sql += " from xx_g00_canteen_data a,personnel_basic b "
    sql += " where 1 = 1 "
    sql += " and a.post_flag = 'N' "
    sql += " and a.resign_flag is null "
    sql += " and a.work_num = b.no "
    callbackFn( res, oracle.connHrm,{sql})
  })

  //抓取离职人员
  router.post("/getDeleteCanteenData", (_req, res) => {
    let sql = " select a.work_num "
    sql += " from xx_g00_canteen_data a "
    sql += " where 1 = 1 "
    sql += " and a.post_flag = 'N' "
    sql += " and a.resign_flag = 'Y' "
    callbackFn( res, oracle.connHrm,{sql})
  })

  //更新已抛
  router.post("/updateCanteenData", (req, res) => {
    const { no } = req.body
    let sql = " update xx_g00_canteen_data "
    sql += " set post_flag = 'Y' "
    sql += " ,updated_date = sysdate "
    sql += " where work_num = '" + no + "' "
    callbackFn( res, oracle.connHrm,{sql})
  })

  return router
}